package com.example.demo.endpoint;

import com.example.demo.entity.UserEntity;
import com.example.demo.service.UserService;
import com.example.demo.util.Msg;
import com.example.demo.util.RedisUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

import cn.hutool.json.JSONObject;
import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("/user")
public class LoginOrRegisterEndpoint {
    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping("/login/returnToken")
    public String getCode(String code) {
        Jedis jedis = new Jedis();
        String token = "";
        token = UUID.randomUUID().toString();
        RedisUtil.setToRedis(token, "token");

        return token;

    }

    @ResponseBody
    @RequestMapping("/login")
    private Msg login(String userName, String passWord) {
        UserEntity user = userService.findByUsername(userName);
        System.out.println(userName);
        Msg msg = new Msg();
        JSONObject jsonObject = new JSONObject();
        if (user == null) {
            msg.setResultCode("401");
            jsonObject.put("result", "用户名错误");
        } else if (!passWord.equals(user.getPassWord())) {
            msg.setResultCode("401");
            jsonObject.put("result", "密码错误");
        } else {
            msg.setResultCode("200");
            jsonObject.put("result", "登录成功");
        }
        msg.setDetailInfo(jsonObject);
        return msg;
    }
}
